/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package talvan;

import java.*;

/**
 * HOW TO: Get Started with Microsoft JDBC
 * http://support.microsoft.com/kb/313100
 *
 * How to configure SQL Server to listen on a specific port
 * http://support.microsoft.com/kb/823938
 *
 * Configuring the Windows Firewall to Allow SQL Server Access
 * http://msdn.microsoft.com/en-us/library/cc646023(v=sql.105).aspx
 *
 * SQL server og JDBC (setup guide from Hans Blaasvær)
 * https://www.dropbox.com/s/iznwa56oh7uzpvt/SQL%20server%20og%20JDBC.pdf?m
 *
 * SQL server 2008 R2 tutorials
 * http://msdn.microsoft.com/en-us/library/ms167593(v=sql.105).aspx
 *
 * Building the Connection URL
 * http://msdn.microsoft.com/en-us/library/ms378428(v=sql.105).aspx
 *
 *
 *
 * @author benjamin
 */
class DbHandler {

    private java.sql.Connection con = null;
    private String url = "jdbc:sqlserver://";
    private String serverName = "localhost";// "tsu.hammerbenjamin.com";
    private String portNumber = "1433";
    private String databaseName = "TALVAN";
    private String userName = "tsu";
    private String password = "1234";//"lm65sBS3xIrlwKI";
    private Boolean integratedSecurity = false;
    // Informs the driver to use server a side-cursor, 
    // which permits more than one active statement 
    // on a connection.
    private String selectMethod = "cursor";

    public DbHandler()
    {}
    
    private String getConnectionUrl() {
        //jdbc:sqlserver://localhost;databaseName=AdventureWorks;integratedSecurity=true;applicationName=MyApp;
        return getUrl() + getServerName() + ":" + getPortNumber() + ";databaseName=" + getDatabaseName() + ";integratedSecurity=" + integratedSecurity + ";";//selectMethod=" + selectMethod + ";";
    }

    public java.sql.Connection getConnection() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = java.sql.DriverManager.getConnection(getConnectionUrl(), getUserName(), getPassword());
            if (con != null) {
                System.out.println("Connection Successful!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error Trace in getConnection() : " + e.getMessage());
        }
        return con;
    }

    


    /*
     * Display the driver properties, database details
     */
    public void displayDbProperties() {
        java.sql.DatabaseMetaData dm = null;
        java.sql.ResultSet rs = null;
        try {
            con = this.getConnection();
            if (con != null) {
                dm = con.getMetaData();
                System.out.println("Driver Information");
                System.out.println("\tDriver Name: " + dm.getDriverName());
                System.out.println("\tDriver Version: " + dm.getDriverVersion());
                System.out.println("\nDatabase Information ");
                System.out.println("\tDatabase Name: " + dm.getDatabaseProductName());
                System.out.println("\tDatabase Version: " + dm.getDatabaseProductVersion());
                System.out.println("Avalilable Catalogs ");
                rs = dm.getCatalogs();
                while (rs.next()) {
                    System.out.println("\tcatalog: " + rs.getString(1));
                }
                rs.close();
                rs = null;
                closeConnection();
            } else {
                System.out.println("Error: No active Connection");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        dm = null;
    }

    public void closeConnection() {
        try {
            if (con != null) {
                con.close();
            }
            con = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Heintar url adressuna til dátagrunnin
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * Setur url adressuna til dátagrunnin
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * Heintar dátagrun navni 
     * @return the serverName
     */
    public String getServerName() {
        return serverName;
    }

    /**
     * Setur Dátagrunn navni
     * @param serverName the serverName to set
     */
    public void setServerName(String serverName) {
        this.serverName = serverName;
    }

    /**
     * heintar Port string
     * @return the portNumber
     */
    public String getPortNumber() {
        return portNumber;
    }

    /**
     * setur port nummari sum forriti skal brúka
     * @param portNumber the portNumber to set
     */
    public void setPortNumber(String portNumber) {
        this.portNumber = portNumber;
    }

    /**
     * @return the databaseName
     */
    public String getDatabaseName() {
        return databaseName;
    }

    /**
     * @param databaseName the databaseName to set
     */
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
    public void resetFields()
    {
        url = "jdbc:sqlserver://";
        serverName = "localhost";// "tsu.hammerbenjamin.com";
        portNumber = "1433";
        databaseName = "TALVAN";
        userName = "tsu";
        password = "1234";//"lm65sBS3xIrlwKI";
    }
}